package com.bwie.spike.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.spike.conf.CreateImageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.spike.entity.MiaoshaActivityEntity;
import com.bwie.spike.service.MiaoshaActivityService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;


/**
 * 秒杀活动表
 *
 * @author lfw
 * @email sunlightcs@gmail.com
 * @date 2022-02-21 20:32:24
 */
@RestController
@RequestMapping("spike/miaoshaactivity")
public class MiaoshaActivityController {
    @Autowired
    private MiaoshaActivityService miaoshaActivityService;

    /**
     * 生成验证码
     *
     * @throws IOException
     */
    @RequestMapping("/getImage")
    public R getImage(HttpServletRequest request) throws IOException {
        CreateImageCode createImageCode = new CreateImageCode();
        //获取验证码
        String securityCode = createImageCode.getCode();
        //验证码存入session
        String key = new SimpleDateFormat("yyyyMMdd").format(new Date());
        //生成图片
        BufferedImage image = createImageCode.getBuffImg();
        //进行base64编码
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", bos);
        String string = Base64Utils.encodeToString(bos.toByteArray());
        return R.ok().put("key",key).put("image",string).put("securityCode",securityCode);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = miaoshaActivityService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MiaoshaActivityEntity miaoshaActivity = miaoshaActivityService.getOne(new QueryWrapper<MiaoshaActivityEntity>().eq("goods_id", id));
        ReentrantLock reentrantLock = new ReentrantLock();
        Condition condition = reentrantLock.newCondition();
        return R.ok().put("activity", miaoshaActivity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MiaoshaActivityEntity miaoshaActivity){
		miaoshaActivityService.save(miaoshaActivity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MiaoshaActivityEntity miaoshaActivity){
		miaoshaActivityService.updateById(miaoshaActivity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		miaoshaActivityService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
